// pages/forget/forget.js
import { Forget } from '../../../models/forget'
const  forgetModel = new Forget()
import {check} from '../../../util/identityCheck'

Page({

  /**
   * 页面的初始数据
   */
  data: {
      sendStatus:true,
      time:60,
      phoneNumber:'',
      sms:'',
      password:'',
      confirmPassword:'',
      sendCode:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      if(!check())return
  },

    setPhoneNumber(event){
        console.log(event.detail)
        this.setData({
            phoneNumber: event.detail
        })
    },

    setSms(event){
        console.log(event.detail)
        this.setData({
            sms: event.detail
        })
    },

    setPassword(event){
        console.log(event.detail)
        this.setData({
            password: event.detail
        })
    },

    setConfirmPassword(event){
        console.log(event.detail)
        this.setData({
            confirmPassword: event.detail
        })
    },
    sendSms(){
        if(!this.data.sendStatus){
            return
        }
        let params = {
            mobile:this.data.phoneNumber,
            sendType:2
        }
        forgetModel.GetSMSCode(params,res=>{
            console.log(res)
            this.switchComputed()
            this.setData({
                sendCode:res.SMSCode,
                time:60
            })
        })
    },

    switchComputed(){
        this.setData({
            sendStatus:false
        })
        let allTime = 60
        let computedTime = setInterval(()=>{
            this.setData({
                time:allTime--
            })
            if(this.data.time===0){
                clearInterval(computedTime)
                this.setData({
                    sendStatus:true
                })
            }
        },1000)
    },

    toast(arg){
        wx.showToast({
            title: arg,
            icon: 'none'
        })
    },

    confirm(){

        if(!this.data.phoneNumber){
            this.toast('您输入的手机号为空！！')
            return
        }

        if(!this.data.code){
            this.toast('你输入的短信验证码为空！！')
            return
        }

        if(!this.data.password){
            this.toast('你输入的密码为空！！')
            return
        }

        if(this.data.password!==this.data.confirmPassword){
            this.toast('两次输入的密码不一致！！')
            return
        }


        let params = {
            username: this.data.phoneNumber,
            newPassword: this.data.password,
            code: this.data.sms,
            sendCode: this.data.sendCode
        }
        forgetModel.GetSetPassword(params,res=>{
            wx.showToast({
                title: '重置成功',
                icon: 'none'
            })
            wx.switchTab({
                url: `/pages/login/login`
            })
        })
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})